package com.example.owner.lettery1.utils;

import android.content.Context;
import android.support.v4.app.FragmentManager;

import com.example.owner.lettery1.R;

import com.example.owner.lettery1.modlus.ProgressDialogFragment;


public class DialogUtil {

    /**
     * 通用进度条弹框DIALOG_TAG
     */
    public final static String DIALOG_TAG = "DIALOG_TAG";

    /**
     * 显示进度弹框
     *
     * @param manager 碎片管理器
     * @param context 上下文
     * @see DialogUtil#showProgressDialog(FragmentManager, Context, int, boolean, boolean)
     */
    public static void showProgressDialog(FragmentManager manager, Context context){
        showProgressDialog(manager, context, R.string.on_loading, true, false);
    }

    /**
     * 显示进度弹框
     *
     * @param manager 碎片管理器
     * @param context 上下文
     * @see DialogUtil#showProgressDialog(FragmentManager, Context, int, boolean, boolean)
     */
    public static void showProgressDialog(FragmentManager manager, Context context, int msg){
        showProgressDialog(manager, context, msg, true, false);
    }

    /**
     * 显示进度弹框
     *
     * @param manager 碎片管理器
     * @see DialogUtil#showProgressDialog(FragmentManager, Context, int, boolean, boolean)
     */
    public static void showProgressDialog(FragmentManager manager, String msg){
        showProgressDialog(manager, msg, true, false);
    }

    /**
     * 显示进度弹框
     * @param manager 碎片管理器
     * @param context 上下文
     * @param strId 文本id
     * @param cancelable 是否可取消
     * @param cancelOutside 点击弹框之外的区域是否取消
     */
    public static void showProgressDialog(FragmentManager manager, Context context, int strId,
                                          boolean cancelable, boolean cancelOutside){
        showProgressDialog(manager, context.getString(strId), cancelable, cancelOutside);
    }



    /**
     * 显示进度弹框
     * @param manager 碎片管理器
     * @param msg 需要显示在进度条上的文本
     * @param cancelable 是否可以被取消
     */
    public static void showProgressDialog(FragmentManager manager, String msg, boolean cancelable, boolean cancelOutside){
        if(manager==null) {

            return;
        }
        ProgressDialogFragment f = (ProgressDialogFragment) manager.findFragmentByTag(DIALOG_TAG);
        if(f!=null){
            f.dismissAllowingStateLoss();
        }
        f = ProgressDialogFragment.Companion.newInstance(msg, cancelable, cancelOutside);
        f.show(manager, DIALOG_TAG);
    }

    /**
     * 隐藏进度弹框
     * @param manager 碎片管理器
     */
    public static void hideProgressDialog(FragmentManager manager){
        if(manager==null) {

            return;
        }
        ProgressDialogFragment f = (ProgressDialogFragment) manager.findFragmentByTag(DIALOG_TAG);
        if(f!=null) f.dismissAllowingStateLoss();
    }


}
